package praktikumid.w09.p2;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

/**
 * Separate file in a separate file. Handle "draw oval"
 * button events.
 * @author Ago
 *
 */
public class DrawOvalListener implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		// getSource() returns the component where
		// the event happened, in our case JButton
		JButton b = (JButton) e.getSource();
		//System.out.println(e.getSource());
		// getParent() returns reference to the container component
		// in our case, JButton is inside the ButtonPanel
		ButtonPanel bp = (ButtonPanel) b.getParent();
		//MyFrame f = bp.frame;
		// to get the frame, we need to get several levels
		// of parent. 
		// ButtonPanel is inside JPanel (framme's ContentPane)
		System.out.println(bp.getParent());
		// JPanel is inside JLayeredPanel
		System.out.println(bp.getParent().getParent());
		// JLayeredPane is inside JRootPane
		System.out.println(bp.getParent().getParent().getParent());
		// JRootPane is inside JFrame
		MyFrame f = (MyFrame) bp.getParent().getParent().getParent().getParent();
		// let's use getContentPane() to get content pane
		// from there, we find the second child component
		// (the first added element is with index 0 etc.)
		DrawPanel dp = (DrawPanel) f.getContentPane().getComponent(1);
		
		// as you can figure out, there are some pointless getParent()
		// calls. The following line does the same as the last
		// 2 code lines:
		//DrawPanel dp = (DrawPanel) bp.getParent().getComponent(1);
		
		dp.drawOval();
	}

}
